package com.five.dao.impl.backstage;

import com.five.dao.backstage.GustDao;
import com.five.entity.backstage.Gust;
import com.five.util.DBUtil;

import java.util.List;
import java.util.Map;

/**
 * @Author: 王登峰
 * @Description:
 * @Date: 2020/11/11 17:21
 */
public class GustDaoImpl implements GustDao {

    @Override
    public List<Map> selectGust(String page, String limit, Gust gust) {
        String sql = "select * from t_user where 1=1 ";
        if (gust.getId()!=null){
            sql+=" and id="+gust.getId()+" ";
        }
        if (gust.getName()!=null){
            sql+=" and name like '%"+gust.getName()+"%' ";
        }
        sql+=" limit ?,?";
        int pp = Integer.parseInt(page);
        int ll = Integer.parseInt(limit);
        return DBUtil.queryMap(sql,(pp-1)*ll,ll);
    }

    @Override
    public int addGust(Gust gust) {
        String sql = "insert into t_user(name,pwd,idcard) values(?,?,?)";
        return DBUtil.manage(sql,gust.getName(),gust.getPwd(),gust.getIdcard());
    }

    @Override
    public int delGust(Gust gust) {
        String sql = "delete from t_user where id = ?";
        return DBUtil.manage(sql,gust.getId());
    }

    @Override
    public Map selectGustById(Gust gust) {
        String sql = "select * from t_user where id =?";
        return  DBUtil.queryMap(sql,gust.getId()).get(0);
    }

    @Override
    public int update(Gust gust) {
        String sql = "update t_user set name = ?,pwd=?,idcard=?,status=0 where id =?";
        return DBUtil.manage(sql,gust.getName(),gust.getPwd(),gust.getIdcard(),gust.getId());
    }

    @Override
    public int getcount(Gust gust) {
        String sql="select count(*) a from t_user where 1=1 ";
        if (gust.getId()!=null){
            sql+=" and id="+gust.getId()+" ";
        }
        if (gust.getName()!=null){
            sql+=" and name like '%"+gust.getName()+"%' ";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        String s = maps.get(0).get("a") + "";
        int i = Integer.parseInt(s);
        return i;
    }
    @Override
    public int bad(Integer id) {
        String sql="update t_user set status=1 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

    @Override
    public int good(Integer id) {
        String sql="update t_user set status=0 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }


}
